<template>
  <div class="article-view-bg">
    <el-card class="article-view-card">
      <div class="article-header">
        <el-button
          class="back-btn"
          type="primary"
          link
          icon="el-icon-arrow-left"
          @click="goBack"
        >返回</el-button>
        <div class="article-time-info">
          <span>创建时间：{{ formatTime(article.created_at) }}</span>
          <span style="margin-left: 24px;">修改时间：{{ formatTime(article.updated_at) }}</span>
          <span style="margin-left: 24px;">浏览：{{ article.view_count }}</span>
        </div>
      </div>
      <h1 class="article-title">{{ article.title }}</h1>
      <div class="article-meta">
        <div class="article-tags">
          <el-tag
            v-for="tag in article.tags"
            :key="tag.id"
            type="info"
            class="article-tag"
          >
            {{ tag.name }}
          </el-tag>
        </div>
      </div>
      <el-card class="article-content-card">
        <div v-html="article.content" class="article-content"></div>
      </el-card>
    </el-card>
  </div>
</template>

<script setup>
import { ref, onMounted } from 'vue'
import { useRoute, useRouter } from 'vue-router'
import { fetchArticle } from '@/api/article'

const route = useRoute()
const router = useRouter()
const article = ref({
  title: '',
  tags: [],
  created_at: '',
  updated_at: '',
  view_count: 0,
  content: ''
})

const formatTime = (time) => {
  if (!time) return ''
  const d = new Date(time)
  const pad = n => n < 10 ? '0' + n : n
  return `${d.getFullYear()}-${pad(d.getMonth() + 1)}-${pad(d.getDate())} ${pad(d.getHours())}:${pad(d.getMinutes())}:${pad(d.getSeconds())}`
}

const loadArticle = async () => {
  const id = route.query.id
  if (id) {
    const res = await fetchArticle(id)
    article.value = res.data
  }
}

const goBack = () => {
  router.back()
}

onMounted(loadArticle)
</script>

<style scoped>
.article-view-bg {
  min-height: 100vh;
  background: #f6f8fa;
  padding: 40px 0;
}
.article-view-card {
  max-width: 900px;
  margin: 0 auto;
  border-radius: 12px;
  box-shadow: 0 4px 24px 0 rgba(0,0,0,0.07);
  padding: 32px 36px 24px 36px;
}
.article-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  height: 40px;
  margin-bottom: 8px;
}
.back-btn {
  font-size: 16px;
  padding-left: 0;
  transition: background 0.2s, color 0.2s;
}
.back-btn:hover {
  background: #e6f0fa !important;
  color: #1976d2 !important;
}
.article-time-info {
  color: #888;
  font-size: 14px;
  display: flex;
  align-items: center;
}
.article-title {
  font-size: 2.2rem;
  font-weight: bold;
  margin-bottom: 18px;
}
.article-meta {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  margin-bottom: 18px;
  justify-content: flex-start;
}
.article-tags {
  display: flex;
  gap: 8px;
}
.article-tag {
  font-size: 14px;
}
.article-content-card {
  margin-top: 18px;
  background: #fff;
  border-radius: 8px;
  box-shadow: 0 2px 8px 0 rgba(0,0,0,0.04);
}
.article-content {
  min-height: 200px;
  font-size: 17px;
  color: #222;
  line-height: 1.8;
  padding: 18px 8px 8px 8px;
}
</style> 